package com.icss.head.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.common.util.ConstatFinalUtil;
import com.icss.service.IUsersDbService;

/**
 * 创建一个过滤器的步骤和Servlet一样 实现一个Filter接口
 * 
 * @author 陈岩
 */
@WebFilter(urlPatterns = "/*")
public class AllHeadFilter implements Filter
{
	@Override
	public void destroy()
	{
		ConstatFinalUtil.SYS_LOGGER.info("==AllHeadFilter==destroy=");
	}

	/**
	 * 过滤器所做的事情=====Servlet中的Service
	 */
	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException
	{
		/*
		 * 虽然参数中指定的是ServletRequest,但是通过运行时打印发现: req就是HttpServletRequest;
		 */
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		
		request.setCharacterEncoding("UTF-8");
		
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig config) throws ServletException
	{
		ConstatFinalUtil.SYS_LOGGER.info("==AllHeadFilter==init=");
	}

}
